package com.example.asuper.check;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.asuper.check.Db.CheckDAL;
import com.example.asuper.check.Helper.SharePrefenceHelper;
import com.example.asuper.check.Model.CheckModel;
import com.example.asuper.check.Model.TitleMessageModel;
import com.example.asuper.check.SdcardManage.FileService;
import com.example.asuper.check.ViewBadger.BadgeView;

import java.util.ArrayList;
import java.util.Date;

public class MainActivity extends Activity implements View.OnClickListener {
    private CheckDAL checkService;
    private ImageButton btn_checking;
    private ImageButton btn_checked;
    private ImageButton btn_uncheck;
    private ImageButton btn_overage;
    private ImageButton btn_sync;
    private ImageButton btn_test;
    private TextView tv_batch;
    private BadgeView badge_checked;
    private BadgeView badge_uncheck;
    private BadgeView badge_overage;
    private BadgeView badge_checking;
    private CheckDAL mCheckDAL;
    private ProgressDialog myDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_checking = (ImageButton) findViewById(R.id.bt_checking);
        btn_test = (ImageButton) findViewById(R.id.imgbtn_test);
        btn_checked = (ImageButton) findViewById(R.id.bt_checked);
        btn_uncheck = (ImageButton) findViewById(R.id.bt_uncheck);
        btn_overage = (ImageButton) findViewById(R.id.bt_overage);
        btn_sync = (ImageButton) findViewById(R.id.bt_sync);
        tv_batch = (TextView) findViewById(R.id.tv_main_batch);
        btn_checking.setOnClickListener(this);
        btn_test.setOnClickListener(this);
        btn_checked.setOnClickListener(this);
        btn_uncheck.setOnClickListener(this);
        btn_overage.setOnClickListener(this);
        btn_sync.setOnClickListener(this);

        badge_checking=new BadgeView(this, btn_checking);
        badge_checked = new BadgeView(this, btn_checked);
        badge_uncheck = new BadgeView(this, btn_uncheck);
        badge_overage = new BadgeView(this, btn_overage);
        badge_checking.setBadgePosition(BadgeView.POSITION_CENTER);
        badge_checked.setBadgePosition(BadgeView.POSITION_CENTER);
        badge_uncheck.setBadgePosition(BadgeView.POSITION_CENTER);
        badge_overage.setBadgePosition(BadgeView.POSITION_CENTER);
        badge_checking.setTextSize(18);
        badge_checked.setTextSize(18);
        badge_uncheck.setTextSize(18);
        badge_overage.setTextSize(18);

        String SpliteCode = SharePrefenceHelper.ReadSharedPreferences("splite_info", "SPLITE", this);
        if (SpliteCode.equals("")) {
            SpliteCode = "\\|";
            SharePrefenceHelper.WriteSharedPreferences("splite_info", "SPLITE", SpliteCode, this);
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        RefreshDate();
        InitCountIcon();
        ReadSharedPreferences();
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.imgbtn_test://测试页面
                intent = new Intent(MainActivity.this, AddActivity.class);
                startActivity(intent);
                break;
            case R.id.bt_checking://盘点页面
                intent = new Intent(MainActivity.this, ScanCodeAndCheckActivity.class);
                startActivity(intent);
                break;
            case R.id.bt_checked://已盘页面
                intent = new Intent(MainActivity.this, Checkedinfo.class);
                startActivity(intent);
                break;
            case R.id.bt_uncheck://未盘页面
                intent = new Intent(MainActivity.this, Uncheckinfo.class);
                startActivity(intent);
                break;
            case R.id.bt_overage://盘盈页面
                intent = new Intent(MainActivity.this, Overageinfo.class);
                startActivity(intent);
                break;
            case R.id.bt_sync://同步页面
                intent = new Intent(MainActivity.this, Syncdate.class);
                startActivity(intent);
                break;
        }
    }
    //更新数据
   private void RefreshDate(){
       boolean testResult=new FileService().isExistsFile("waitcheck.txt");
       if (testResult){
           myDialog=ProgressDialog.show(MainActivity.this,"同步数据","Loading.....",true);
           new Thread(){
               public void run(){
                   try{
                       boolean boolresult = inputDate();//导入数据
                       if (boolresult){
                           boolresult=  new FileService().DeleteFile("waitcheck.txt");
                       }
                       if (boolresult) {
                           SharedPreferences user = getSharedPreferences("batch_info", Context.MODE_PRIVATE);
                           SharedPreferences.Editor editor = user.edit();
                           editor.putString("BATCH",new Date().toString());
                           editor.commit();
                       }
                       //showText="导入成功！";
                       //myDialog.setMessage("导入成功");

                   }catch ( Exception ex){
                       ex.printStackTrace();
                   }
                   finally {
                       myDialog.dismiss();

                   }
               }
           }.start();
       }
   }
    //初始化角标
    private void InitCountIcon() {
        checkService = new CheckDAL(this);
        TitleMessageModel titleMsg = checkService.getTitleMessage();

        //已盘
        String checkedCount = (titleMsg.getCheckedCount() == null || titleMsg.getCheckedCount().toString().equals("")) ? "0" : titleMsg.getCheckedCount();
        badge_checked.setText(checkedCount);
//        badge_checked.setTextColor(Color.WHITE);
//        badge_checked.setBadgeBackgroundColor(Color.GREEN);
        badge_checked.show();

        //未盘
        String uncheckCount = (titleMsg.getUncheckCount() == null || titleMsg.getUncheckCount().toString().equals("")) ? "0" : titleMsg.getUncheckCount();
        badge_uncheck.setText(uncheckCount);
        badge_uncheck.show();

        //盘盈
        String overageCount = (titleMsg.getOverageCount() == null || titleMsg.getOverageCount().toString().equals("")) ? "0" : titleMsg.getOverageCount();
        badge_overage.setText(overageCount);
        badge_overage.show();

        //总数
        String checkingCount =String.valueOf(Integer.parseInt(checkedCount)+Integer.parseInt(uncheckCount)+Integer.parseInt(overageCount));
        badge_checking.setText(checkingCount);
        badge_checking.show();

    }

    //读取批次信息
    private boolean ReadSharedPreferences() {
        try {
            String batchNum;
            SharedPreferences user = getSharedPreferences("batch_info", Context.MODE_PRIVATE);
            batchNum = user.getString("BATCH", "");
            if (batchNum != null && !batchNum.equals("")) {
                tv_batch.setText("当前批次：" + batchNum);
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

    //导入数据
    private boolean inputDate() {
        boolean addResult = false;

        FileService fileService = new FileService(this);
        ArrayList<CheckModel> result = null;
        result = fileService.getFile("waitcheck.txt");

        if (result.size() > 0) {
            mCheckDAL = new CheckDAL(this);
            if (mCheckDAL.deleteCheckData("", "2")) {
                addResult = mCheckDAL.addCheckData(result);
            } else {
                addResult = false;
            }
        }
        // Toast.makeText(this, addResult ? "导入成功！" : "导入失败！", Toast.LENGTH_LONG).show();
        return addResult;
    }

}
